var createError = require("http-errors");
var express = require("express");
var path = require("path");
var cookieParser = require("cookie-parser");
var logger = require("morgan");
var fs = require('fs')
//小宝组件  前端
var xiaobao = require("./routes/xiaobao/api/index");
//小宝业主后端业务总查询
const xiaobaoapi = require("./routes/xiaobao/yezhu/api");

//登录接口
var login = require("./routes/login");

//添加业务从这里开始   第一步     改名       路径名成
//四川海哥  前端
var schg = require("./routes/schg/api/index");
//小宝业主后端业务总查询
var schgapi = require("./routes/schg/yezhu/api");

//获取官方区块网站
var gf = require("./src/axios/index");
//聊天室功能
var chat = require("./routes/chat/api/index");
var cors = require("cors");
const { Stream } = require("stream");
var app = express();
app.use(cors());
// view engine setup
app.set("views", path.join(__dirname, "views"));
app.set("view engine", "jade");
//日志系统   监控系统    sentry   你的软件   bug 错日志 通过监控能查询处理
// const log = fs.createWriteStream("access.log",{flags:"a"})
// app.use(logger("combined",{stream:log}));
app.use(logger("dev"));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, "public")));

//用户登录
app.use("/", login);

//小宝业主     先不用front  以后其他都要改
app.use("/xiaobao", xiaobao);
//小宝业主  通过表中的云服务器ip地址获取新的区块然后写入表中
app.use("/xiaobao", xiaobaoapi);

//四川海哥业主     路由应用

app.use("/schg", schg);
//四川海哥  通过表中的云服务器ip地址获取新的区块然后写入表中
app.use("/schg", schgapi);

//查询官方高度区块
app.use("/gf", gf);
//聊天室功能
app.use("/chat", chat);

// catch 404 and forward to error handler
app.use(function (req, res, next) {
  next(createError(404));
});

// error handler
app.use(function (err, req, res, next) {
  // set locals, only providing error in development
  res.locals.message = err.message;
  res.locals.error = req.app.get("env") === "development" ? err : {};

  // render the error page
  res.status(err.status || 500);
  res.render("error");
});



module.exports = app;
